import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class NGuessNumberTest {
    @Before
    public void setUp()  {
        System.out.println("测试开始");
    }

    @After
    public void tearDown()  {
        System.out.println("测试结束");
    }

    @Test
    public void generateAnswerTest() {

        NGuessNumber n = new NGuessNumber();

        int[] arr = n.generateAnswer();
        assertEquals("数字数量不为4",4,arr.length);
        System.out.println("数字数量为4");

        boolean flag = false;//数字是否重复
        boolean flag2 = true;//数字是否在范围内
        for (int i = 0 ; i < arr.length ; i++){
            if(arr[i]>=0&&arr[i]<=9){
                flag2 = false;
                break;
            }

            for (int j = 0 ; j < arr.length ;j++){
                if (i!=j){
                    if (arr[i] == arr[j]){
                        flag = true;
                        break;
                    }
                }
            }

            if (flag){
                break;
            }
        }

        assertEquals("有数字不在0-9之间",flag2);
        System.out.println("数字都在0-9之间");

        assertEquals("数组内数字有重复",flag);
        System.out.println("数组内数字不重复");
    }
    @Test
    public void getPlayerInputTest() {
        NGuessNumber n = new NGuessNumber();
        int[] arr = n.getPlayerInput();
        assertEquals("用户输入的数字数量不为4",4,arr.length);
        System.out.println("用户输入的数字数量为4");
    }
    @Test
    public void compareGuessAnswerTest() {
        NGuessNumber n = new NGuessNumber();
        int[] arr = n.generateAnswer();
        int[] playerArr = n.getPlayerInput();

        int a = 0;
        int b = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == playerArr[i]) {
                a++;
            }

            for (int j = 0; j < arr.length; j++) {
                if (i != j) {
                    if (arr[i] == playerArr[j]) {
                        b++;
                    }
                }
            }
        }

        for (int i : arr) {
            System.out.print(i);
        }
        System.out.println();
        for (int i : playerArr) {
            System.out.print(i);
        }
        System.out.println();
        System.out.println("程序结果"+n.compareGuessAnswer()[0]+"\t"+n.compareGuessAnswer()[1]);
        System.out.println("测试结果"+a+"\t"+b);
        assertEquals("比较结果a出错", n.compareGuessAnswer()[0], a);
        assertEquals("比较结果b出错", n.compareGuessAnswer()[1], b);
        System.out.println("比较结果正确");
    }
    @Test
    public void isWinTest() {
        NGuessNumber n = new NGuessNumber();
        int[] arr = n.setArr(new int[]{1,2,3,4});
        int[] playerArr = n.getPlayerInput();

        int a = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] ==playerArr[i]){
                a++;
            }
        }

        n.compareGuessAnswer();
        assertTrue("胜利判定出错", n.isWin());
        System.out.println("胜利判定正确");
    }

}